<!DOCTYPE html>
<html lang="zh">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
	<meta name="keywords" content="dromara.org" />
	<meta name="description" content="dromara.org" />
	<!-- 网页标签标题 -->
	<title>dromara(Open source organization)</title>
	<link rel="shortcut icon" href="/website/img/docsite.ico"/>
	<link rel="stylesheet" href="/website/build/documentation.css" />
</head>
<body>
	<div id="root"><div class="documentation-page" data-reactroot=""><header class="header-container header-container-normal"><div class="header-body"><a href="/website/zh-cn/index.html"><img class="logo" src="/website/img/dromara.png"/></a><div class="search search-normal"><span class="icon-search"></span></div><span class="language-switch language-switch-normal">En</span><div class="header-menu"><img class="header-menu-toggle" src="/website/img/system/menu_gray.png"/><ul><li class="menu-item menu-item-normal"><a href="/website/zh-cn/index.html" target="_self">首页</a><div class="sub-menu"><ul></ul></div></li><li class="menu-item menu-item-normal menu-item-normal-active"><a href="#">文档</a><div class="sub-menu"><ul><li><a href="/website/zh-cn/docs/soul/soul.html" target="_self">Soul</a></li><li><a href="/website/zh-cn/docs/hmily/index.html" target="_self">Hmily</a></li></ul></div></li><li class="menu-item menu-item-normal"><a href="/website/zh-cn/blog/index.html" target="_self">博客</a><div class="sub-menu"><ul></ul></div></li><li class="menu-item menu-item-normal"><a href="/website/zh-cn/community/index.html" target="_self">社区</a><div class="sub-menu"><ul></ul></div></li></ul></div></div></header><section class="content-section"><div class="sidemenu" id="menu"><div class="sidemenu-toggle"><img src="https://img.alicdn.com/tfs/TB1E6apXHGYBuNjy0FoXXciBFXa-200-200.png"/></div><ul><li class="menu-item menu-item-level-1"><span>Soul</span><ul><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/website/zh-cn/docs/soul/soul.html" target="_self">soul介绍</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/website/zh-cn/docs/soul/team.html" target="_self">团队介绍</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/website/zh-cn/docs/soul/induction.html" target="_self">soul极简入门</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/website/zh-cn/docs/soul/db.html" target="_self">数据库设计</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/website/zh-cn/docs/soul/config.html" target="_self">数据配置流程</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/website/zh-cn/docs/soul/dataSync.html" target="_self">数据同步原理</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/website/zh-cn/docs/soul/metaData.html" target="_self">元数据概念介绍</a></li><li style="height:684px;overflow:hidden" class="menu-item menu-item-level-2"><span>用户使用文档<img style="transform:rotate(0deg)" class="menu-toggle" src="/website/img/system/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/setup.html" target="_self">搭建soul网关环境</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/user-http.html" target="_self">http用户</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/user-dubbo.html" target="_self">dubbo用户</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/user-springcloud.html" target="_self">springCloud用户</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/user-sofa.html" target="_self">sofa用户</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/selector.html" target="_self">选择器规则详解</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/user-dataSync.html" target="_self">数据同步策略</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/plugin-divide.html" target="_self">divide插件</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/plugin-dubbo.html" target="_self">dubbo插件</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/plugin-springcloud.html" target="_self">springcloud插件</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/plugin-sofa.html" target="_self">sofa插件</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/plugin-rateLimiter.html" target="_self">rateLimiter插件</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/plugin-hystrix.html" target="_self">hystrix插件</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/plugin-monitor.html" target="_self">monitor插件</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/plugin-waf.html" target="_self">waf插件</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/plugin-sign.html" target="_self">sign插件</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/plugin-rewrite.html" target="_self">rewrite插件</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/plugin-websocket.html" target="_self">websocket支持</a></li></ul></li><li style="height:360px;overflow:hidden" class="menu-item menu-item-level-2"><span>开发者文档<img style="transform:rotate(0deg)" class="menu-toggle" src="/website/img/system/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/dev-filter.html" target="_self">自定义filter</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/dev-plugin.html" target="_self">自定义插件</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/dev-file.html" target="_self">文件上传下载</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/dev-iphost.html" target="_self">自定义解析IP与host</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/dev-result.html" target="_self">自定义返回结果</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/dev-sign.html" target="_self">自定义签名插件算法与验证</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/dev-client.html" target="_self">多语言http客户端接入</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/dev-thread.html" target="_self">线程模型</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/dev-netty.html" target="_self">soul调优</a></li></ul></li><li style="height:144px;overflow:hidden" class="menu-item menu-item-level-2"><span>社区贡献<img style="transform:rotate(0deg)" class="menu-toggle" src="/website/img/system/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/contributor.html" target="_self">贡献者指南</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/committer.html" target="_self">提交者指南</a></li><li class="menu-item menu-item-level-3"><a href="/website/zh-cn/docs/soul/code-conduct.html" target="_self">编码规范</a></li></ul></li></ul></li></ul></div><div class="doc-content markdown-body" id="doc-body"><h2>说明</h2>
<ul>
<li>
<p>本文旨在帮助http用户。</p>
</li>
<li>
<p>soul网关使用 divide 插件来处理http请求。请求在soul-admin后台开启它。</p>
</li>
<li>
<p>接入前，请正确的启动 <code>soul-admin</code> , 以及 <a href="setup.md">搭建环境</a> OK。</p>
</li>
</ul>
<h2>引入网关对http的代理插件</h2>
<ul>
<li>在网关的 <code>pom.xml</code> 文件中增加如下依赖：</li>
</ul>
<pre><code class="language-xml">  <span class="hljs-comment">&lt;!--if you use http proxy start this--&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.dromara<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>soul-spring-boot-starter-plugin-divide<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>${last.version}<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>

   <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.dromara<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>soul-spring-boot-starter-plugin-httpclient<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>${last.version}<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
</code></pre>
<ul>
<li>当然是要重新启动网关。</li>
</ul>
<h2>Http请求接入网关（springMvc体系用户）</h2>
<ul>
<li>首先要确保在 <code>soul-admin</code> 后台 divide插件是否开启。</li>
</ul>
<h5>Soul-Client接入方式。 （此方式针对SpringMvc,SpringBoot用户）</h5>
<ul>
<li>
<p>SpringBoot用户</p>
<ul>
<li>在你的真实服务的 <code>pom.xml</code> 新增如下依赖:</li>
</ul>
</li>
</ul>
<pre><code class="language-xml">     <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.dromara<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>soul-spring-boot-starter-client-springmvc<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>${last.version}<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
     <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
</code></pre>
<ul>
<li>在yml中新增如下配置 ：</li>
</ul>
<pre><code class="language-yaml">   <span class="hljs-attr">soul:</span>
     <span class="hljs-attr">http:</span>
       <span class="hljs-attr">adminUrl:</span> <span class="hljs-string">http://localhost:9095</span>
       <span class="hljs-attr">port:</span> <span class="hljs-string">你本项目的启动端口</span>
       <span class="hljs-attr">contextPath:</span> <span class="hljs-string">/http</span>
       <span class="hljs-attr">appName:</span> <span class="hljs-string">http</span>
       <span class="hljs-attr">full:</span> <span class="hljs-literal">false</span>  
   <span class="hljs-comment"># adminUrl: 为你启动的soul-admin 项目的ip + 端口，注意要加http://</span>
   <span class="hljs-comment"># port: 你本项目的启动端口</span>
   <span class="hljs-comment"># contextPath: 为你的这个mvc项目在soul网关的路由前缀，这个你应该懂意思把？ 比如/order ，/product 等等，网关会根据你的这个前缀来进行路由.</span>
   <span class="hljs-comment"># appName：你的应用名称，不配置的话，会默认取 `spring.application.name` 的值</span>
   <span class="hljs-comment"># full: 设置true 代表代理你的整个服务，false表示代理你其中某几个controller</span>
</code></pre>
<ul>
<li>SpringMvc用户
<ul>
<li>在你的真实服务的 <code>pom.xml</code> 新增如下依赖:</li>
</ul>
</li>
</ul>
<pre><code class="language-xml">       <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
           <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.dromara<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
           <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>soul-client-springmvc<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
           <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>${last.version}<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
       <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
</code></pre>
<ul>
<li>在你的 bean定义的xml文件中新增如下 ：</li>
</ul>
<pre><code class="language-xml">   <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span> =<span class="hljs-string">"springMvcClientBeanPostProcessor"</span> <span class="hljs-attr">class</span> =<span class="hljs-string">"org.dromara.soul.client.springmvc.init.SpringMvcClientBeanPostProcessor"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">constructor-arg</span>  <span class="hljs-attr">ref</span>=<span class="hljs-string">"soulSpringMvcConfig"</span>/&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>
   
   <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"soulSpringMvcConfig"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"org.dromara.soul.client.springmvc.config.SoulSpringMvcConfig"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"adminUrl"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"http://localhost:9095"</span>/&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"port"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"你的端口"</span>/&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"contextPath"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"/你的contextPath"</span>/&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"appName"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"你的名字"</span>/&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"full"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"false"</span>/&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>
</code></pre>
<ul>
<li>
<p>在你的 <code>controller</code> 的接口上加上 <code>@SoulSpringMvcClient</code> 注解。</p>
<ul>
<li>
<p>你可以把注解加到 <code>Controller</code> 类上面, 里面的path属性则为前缀，如果含有 <code>/**</code> 代表你的整个接口需要被网关代理。</p>
</li>
<li>
<p>举列子 （1）： 代表 <code>/test/payment</code>, <code>/test/findByUserId</code> 都会被网关代理。</p>
</li>
</ul>
</li>
</ul>
<pre><code class="language-java"> <span class="hljs-meta">@RestController</span>
 <span class="hljs-meta">@RequestMapping</span>(<span class="hljs-string">"/test"</span>)
 <span class="hljs-meta">@SoulSpringMvcClient</span>(path = <span class="hljs-string">"/test/**"</span>)
 <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HttpTestController</span> </span>{
     
     <span class="hljs-meta">@PostMapping</span>(<span class="hljs-string">"/payment"</span>)
     <span class="hljs-function"><span class="hljs-keyword">public</span> UserDTO <span class="hljs-title">post</span><span class="hljs-params">(@RequestBody <span class="hljs-keyword">final</span> UserDTO userDTO)</span> </span>{
         <span class="hljs-keyword">return</span> userDTO;
     }
  
     <span class="hljs-meta">@GetMapping</span>(<span class="hljs-string">"/findByUserId"</span>)
     <span class="hljs-function"><span class="hljs-keyword">public</span> UserDTO <span class="hljs-title">findByUserId</span><span class="hljs-params">(@RequestParam(<span class="hljs-string">"userId"</span>)</span> <span class="hljs-keyword">final</span> String userId) </span>{
         UserDTO userDTO = <span class="hljs-keyword">new</span> UserDTO();
         userDTO.setUserId(userId);
         userDTO.setUserName(<span class="hljs-string">"hello world"</span>);
         <span class="hljs-keyword">return</span> userDTO;
     }      
  }
</code></pre>
<ul>
<li>举列子 （2）： 代表 <code>/order/save</code>,会被网关代理,而<code>/order/findById</code> 则不会。</li>
</ul>
<pre><code class="language-java"> <span class="hljs-meta">@RestController</span>
 <span class="hljs-meta">@RequestMapping</span>(<span class="hljs-string">"/order"</span>)
 <span class="hljs-meta">@SoulSpringMvcClient</span>(path = <span class="hljs-string">"/order"</span>)
 <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">OrderController</span> </span>{
 
     <span class="hljs-meta">@PostMapping</span>(<span class="hljs-string">"/save"</span>)
     <span class="hljs-meta">@SoulSpringMvcClient</span>(path = <span class="hljs-string">"/save"</span>)
     <span class="hljs-function"><span class="hljs-keyword">public</span> OrderDTO <span class="hljs-title">save</span><span class="hljs-params">(@RequestBody <span class="hljs-keyword">final</span> OrderDTO orderDTO)</span> </span>{
         orderDTO.setName(<span class="hljs-string">"hello world save order"</span>);
         <span class="hljs-keyword">return</span> orderDTO;
     }

     <span class="hljs-meta">@GetMapping</span>(<span class="hljs-string">"/findById"</span>)
     <span class="hljs-function"><span class="hljs-keyword">public</span> OrderDTO <span class="hljs-title">findById</span><span class="hljs-params">(@RequestParam(<span class="hljs-string">"id"</span>)</span> <span class="hljs-keyword">final</span> String id) </span>{
         OrderDTO orderDTO = <span class="hljs-keyword">new</span> OrderDTO();
         orderDTO.setId(id);
         orderDTO.setName(<span class="hljs-string">"hello world findById"</span>);
         <span class="hljs-keyword">return</span> orderDTO;
     }
 }
</code></pre>
<ul>
<li>启动你的项目，你的接口接入到了网关。</li>
</ul>
<h2>Http请求接入网关（其他语言，非springMvc体系）</h2>
<ul>
<li>
<p>首先在 <code>soul-admin</code> 找到 divide插件，进行选择器，和规则的添加，进行流量的匹配筛选。</p>
</li>
<li>
<p>如果不懂怎么配置，请看选择，规则介绍 <a href="selector.md">选择器规则介绍</a>。</p>
</li>
<li>
<p>您也可以自定义开发属于你的 http-client，参考 <a href="dev-client.md">多语言Http客户端开发</a>。</p>
</li>
</ul>
<h2>用户请求</h2>
<ul>
<li>
<p>说白了，你之前怎么请求就怎么请求，没有很大的变动，变动的地方有2点。</p>
</li>
<li>
<p>第一点，你之前请求的域名是你自己的服务，现在要换成网关的域名 （这个你听的懂？）</p>
</li>
<li>
<p>第二点，soul网关需要有一个路由前缀，这个路由前缀就是你接入项目进行配置 <code>contextPath</code> ,如果熟的话，可以自由在 <code>soul-admin</code> 中的divide插件进行自由更改.</p>
</li>
</ul>
<pre><code class="language-yaml">
<span class="hljs-comment"># 比如你有一个 order服务 它有一个接口，请求路径 http://localhost:8080/test/save</span>

<span class="hljs-comment"># 现在就需要换成：http://localhost:9195/order/test/save</span>

<span class="hljs-comment"># 其中 localhost:9195 为网关的ip端口，默认端口是9195 ，/order 是你接入网关配置的 contextPath</span>

<span class="hljs-comment"># 其他参数，请求方式不变。</span>

<span class="hljs-comment"># 我讲到这里还不懂？ 请加群问吧</span>

</code></pre>
<ul>
<li>然后你就可以进行访问了，如此的方便与简单。</li>
</ul>
</div></section><footer class="footer-container"><div class="footer-body"><img src="/website/img/dromara.png"/><div class="cols-container"><div class="col col-12"><h3>免责声明</h3><p>任何单位或个人转载本网站的所有相关信息，请注明来源。</p></div><div class="col col-6"><dl><dt>文档</dt><dd><a href="/website/zh-cn/docs/soul/soul.html" target="_self">概览</a></dd><dd><a href="/website/zh-cn/docs/soul/soul.html" target="_self">快速开始</a></dd><dd><a href="/website/zh-cn/docs/soul/soul.html" target="_self">开发者指南</a></dd></dl></div><div class="col col-6"><dl><dt>资源</dt><dd><a href="/website/zh-cn/blog/index.html" target="_self">博客</a></dd><dd><a href="/website/zh-cn/community/index.html" target="_self">社区</a></dd></dl></div></div><div class="copyright"><span>Copyright ©2020 xiaoyu@apache.org by xiaoyu</span></div></div></footer></div></div>
	<script src="https://f.alicdn.com/react/15.4.1/react-with-addons.min.js"></script>
	<script src="https://f.alicdn.com/react/15.4.1/react-dom.min.js"></script>
	<script>
		window.rootPath = '/website';
  </script>
	<script src="/website/build/documentation.js"></script>
</body>
</html>